#!/bin/bash

###########################################################################
#  Variables
#  $1 month
#  $2 year, 4 digit
#  $3 Instrument Name
#  $4 E-mail address
# 
# Usage: filterdata.sh (month) (year) (instrument name) (e-mail address)
###########################################################################

DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

#rough error check to ensure four parameter entries
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]
then
	echo "Missing parameters, please enter using the following schema: filterdata.sh (month) (year) (instrument_name) (e-mail address)"
else
	#read config file
	while read line; do
    	eval $line
	done < ${DIR}/../Config/config
    
	#Filter data files    
	grep "${3}::${1}-"[0-9]*"-${2}" ${DIR}/../Data/${saroad}-${2}* > $saroad-$1-$2-$3_data.txt
    bzip2 ${saroad}-${1}-${2}-${3}_data.txt
#    rm -f ${saroad}-${1}-${2}-${3}_data.txt < /dev/null
    
    
	echo "Data for $saroad for the month of $1, $2 for the $3 are attached." | mutt -s "Monthly Data for $2, month of $1" -a ${saroad}-${1}-${2}-${3}_data.txt.bz2 -- $4
    
	if !(rm -f $saroad-$1-$2-$3_data.txt.bz2 < /dev/null)
	then
		echo "Failure to delete data file"
	fi	
fi